﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using Slja.Utility;
using Slja.BLL;
using Slja.Model;


namespace FrameWork.web.Web.ServiceHandler
{
    /// <summary>
    /// jqueryUploadHandler 的摘要说明
    /// </summary>
    public class jqueryUploadHandler : IHttpHandler
    {
       

        public void ProcessRequest(HttpContext context)
        {

            FileClass UpFileClass = (FileClass)Enum.Parse(typeof(FileClass), context.Request.QueryString["fileclass"]);
            int OrderID = int.Parse(context.Request.QueryString["orderid"]);
            string PrjID = context.Request.QueryString["prjid"];

            context.Response.ContentType = "text/plain";
            HttpPostedFile file = context.Request.Files["FileData"];
            string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\\";
            if (file != null)
            {
                //保存图片
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }

                string tycode = ((int)UpFileClass).ToString("00");
                string newname = PrjID + "_" + tycode + "_" + file.FileName;
                string fullpath = uploadpath + newname;
                file.SaveAs(fullpath);


                //保存记录
                OrderFilesBLL filebll = new OrderFilesBLL();
                OrderFilesModel model = new OrderFilesModel();
                model.enum_FileClass = UpFileClass;
                model.FileClass = (int)UpFileClass;
                model.FileName = file.FileName;
                model.Note = "";
                model.OrderID = OrderID;
                model.OwnUser = Common.Get_UserID;
                model.Path = context.Request["folder"]+"/" + newname;
                model.UploadDate = DateTime.Now;
                int fileid = filebll.Add(model);

                //context.Response.Write(string.Format("<tr><td>{0}</td><td>{1}</td></tr>",file.FileName,file.ContentLength/(8*1024)+"K"));
                context.Response.Write(string.Format("<file><id>{0}</id><path>{1}</path><time>{2}</time><size>{3}</size></file>",
                   fileid,model.Path, model.UploadDate,file.ContentLength / (8 * 1024) + "K"));
            }
            else
            {
                context.Response.Write("0");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}